<template>
	<view>
		<searchBar :searchBgColor="searchBgColor" rightIcon="icon-comment" placeText="我和我的祖国"></searchBar>
		<view :style="{'height':zhanwei+'px'}"></view>
		
		<view class="content">
			<indexSwiper :hasFresh="isFresh" ></indexSwiper>
			<classEnter :hasFresh="isFresh"></classEnter>
			<!-- <image class="grEmpty" mode="widthFix" src="http://s.de123.com/grow/index.png" @tap="toPage" data-url="growthRecord"></image> -->
			<helprukou :homelist="homelist" v-if="homelist.length>0"></helprukou>
			<!-- <famousTeacher :hasFresh="isFresh"></famousTeacher> -->
			<!-- <hot-sale :hasFresh="isFresh"></hot-sale> -->
		</view>
		
		<screenBar ref="screenBar" :cntHeight="cntHeight" :top="top" :isFiexd="isFiexd"></screenBar>
		
		<goodList :goodData="rmdList"></goodList>
		
		<view :style="{'height':hgt+'px'}" v-if="rmdList.length<1">
			<emptyBox :isLoding="loding"></emptyBox>
		</view>
		<hk-modal ref="hkModal"></hk-modal>
		<imagePoup></imagePoup>
		<imageScroll :scType="scType"></imageScroll>
		<!-- 登录的新组件 -->
		<uni-popup ref="popup" type="center" mask-click="false" @touchmove.stop.prevent="moveHandle">
			<newlogin @tap="close"></newlogin>
		</uni-popup>
	</view>
</template>

<script>
	import COMM from "@/common/common.js";
	import searchBar from "@/components/searchBar/searchBar.vue";
	import classEnter from "@/components/classEnter/classEnter.vue";
	// import hotSale from "@/components/hotSale/hotSale.vue";
	import goodList from "@/components/goodList/goodList.vue";
	import screenBar from "@/components/screenBar/screenBar.vue";
	import statusBar from "@/components/statusBar/statusBar.vue";
	import indexSwiper from "@/components/indexSwiper/indexSwiper.vue";
	import emptyBox from "@/components/emptyBox/emptyBox.vue";
	import Request from "@/sdk/pocky-request/index.js";
	import famousTeacher from "@/components/famousTeacher/famousTeacher.vue";
	import imagePoup from "@/components/imagePoup/imagePoup.vue";
	import imageScroll from "@/components/imageScroll/imageScroll.vue";
    import helprukou from "@/components/helprukou/helprukou.vue";
	import {
		mapState,
		mapGetters,
		mapMutation,
		mapActions
	} from "vuex";
	export default {
		components: {
			searchBar,
			classEnter,
			// hotSale,
			goodList,
			screenBar,
			statusBar,
			indexSwiper,
			emptyBox,
			famousTeacher,
			imagePoup,
			imageScroll,
			helprukou
		},
		computed: {
			...mapState({
				sysInfo: state => state.sys.sysInfo,
				locationCity: state => state.map.locationCity,
				initInfo: state => state.sys.initInfo,
				roads: state => state.map.roads,
				selectCity: state => state.map.selectCity,
				parentId:state=>state.user.parentId,
				isLogin: state => state.user.isLogin,
			})

		},
		data() {
			return {
				searchBgColor: '#ffcd11',
				isFiexd: false,
				cntHeight: 0,
				top: 0,
				scHeight: uni.getSystemInfoSync().windowHeight - uni.getSystemInfoSync().windowBottom - uni.upx2px(90),
				isFresh: true,
				hasNext: true,
				page_no: 1,
				hgt: uni.getSystemInfoSync().windowHeight,
				areaId: 0,
				rmdList: [],
				zhanwei: uni.getSystemInfoSync().statusBarHeight + uni.upx2px(90),
				rmdFilter: {
					page_no: 1,
					page_size: 10,
					areaId: 0
				},
				swiperList: [],
				classList: [],
				hotSaleList: [],
				loding:true,
				scType:0,
				homelist:[]
			}
		},

		methods: {
			
			
			open() {
				// 通过组件定义的ref调用uni-popup方法
				this.$refs.popup.open();
				
			},
			
			close() {
				// 通过组件定义的ref调用uni-popup方法
				this.$refs.popup.close();
				
			},
			scView(e) {
				console.log(e.detail.scrollTop);
				if (e.detail.scrollTop >= this.cntHeight || this.$refs.screenBar.show) {
					this.isFiexd = true
				} else {
					this.isFiexd = false
				}
			},
			
			getDatahome() {
				this.$store
					.dispatch('apihomePagePower', {
						cityCode: this.selectCity.cityCode						
					})
					.then(res => {
						// console.log(res);
						this.homelist = res.data.cityPower	
					    // console.log(this.homelist);
					})
			},
			
			getRmd() {
				this.rmdFilter.guideCate=uni.getStorageSync("xinqu");
				if (this.hasNext) {
					this.$store.dispatch("apiGetRmdGoodList", this.rmdFilter).then(res => {
						
						this.rmdList = this.rmdList.concat(res.data.data.items)
						
						this.rmdFilter.page_no = res.data.data.nextPage;
						this.hasNext = res.data.data.hasNext
						this.loding=false;
					})
				} else {
					uni.showToast({
						title: '没有更多数据了',
						icon: 'none'
					});
				}

			},
			

		},
		watch: {
			selectCity(newVal) {
				this.isFresh = false;
				this.hasNext = true;
				this.rmdList = [];
				this.rmdFilter = {
					page_no: 1,
					page_size: 10,
					areaId: newVal.cityCode
				};
				this.getRmd();
				this.isFresh = true;
			}
		},
		onShareAppMessage(res) {
			return {
				title: '找培训·拼好课',
				path: '/pages/index/index',
				imageUrl:'http://s.de123.com/mpshare.png'
			}
		},
		//分享到朋友圈
		onShareTimeline(res){	
				return {
					title: '找培训·拼好课',
					path: '/pages/index/index',
					imageUrl:'http://s.de123.com/mpshare.png'
				}
			
		},
		onLoad(cfm) {
			console.log(cfm.scene)
			// if (cfm.scene) {
				
			// let scene = decodeURIComponent(cfm.scene);
			// console.log(scene)
			
			// //-是我们分隔参数的方式
			
			// let storeId = scene.split("&")[0];
			
			
			//  let userId = scene.split('&')[1];
			 
			// let cid = scene.split('&')[2];
			
			// let actOldAndNewId = scene.split('&')[3];
			
			// this.toPage({
			// 	url:'oldfulifriend',
			// 	storeId:storeId,
			// 	userId:userId,
			// 	cid:cid,
			// 	actOldAndNewId:actOldAndNewId
				
			// })
						
			// }
			this.getDatahome()
			
			
			
			this.rmdFilter.areaId = this.selectCity.cityCode;
			var _this = this;
			if(this.rmdList.length<1){
				this.getRmd()
			}
			uni.$on("roadsChange",()=>{
				this.isFresh = false;
				this.rmdFilter.page_no = 1;
				this.hasNext = true;
				this.rmdFilter.areaId = this.locationCity.adcode.substr(0, 4);
				this.rmdList = [];
				this.loding=true;
				setTimeout(() => {
					this.isFresh = true;
					this.getRmd()
				}, 1500)
			})
			
			// 监听区域选择
			uni.$on("changeArea", payload => {
				this.rmdList = [];
				this.rmdFilter.page_no = 1;
				this.hasNext = true;
				this.rmdFilter.areaId = payload.areaId
				delete this.rmdFilter.streetId
				this.getRmd()
			})
			// 监听商圈选择
			uni.$on("changeStreet", payload => {
				
				this.rmdList = [];
				this.rmdFilter.page_no = 1;
				this.hasNext = true;
				this.rmdFilter.streetId = payload.streetId,
				this.rmdFilter.areaId = payload.areaId.toString()
				this.getRmd()
			})
			// 监听距离排序
			uni.$on("rankChange", payload => {
				this.rmdList = [];
				this.rmdFilter.page_no = 1;
				this.hasNext = true;
				if (payload.type == 0) {
					delete this.rmdFilter.orderBy;
					delete this.rmdFilter.longi;
					delete this.rmdFilter.lati;
				} else {
					this.rmdFilter.orderBy = payload.type
					if (payload.type == 5) {
						this.rmdFilter.longi = this.roads.longitude;
						this.rmdFilter.lati = this.roads.latitude;
					} else {
						delete this.rmdFilter.longi;
						delete this.rmdFilter.lati;
					}
				}
				this.getRmd()
			})
			// 监听分类选择
			uni.$on("classChange", payload => {
				this.rmdList = [];
				this.rmdFilter.page_no = 1;
				this.hasNext = true;
				if (payload.type == 'firCate') {
					delete this.rmdFilter.secCate;
					delete this.rmdFilter.thirCate;
					this.rmdFilter.firCate = payload.firCate;

				} else if (payload.type == 'secCate') {
					delete this.rmdFilter.thirCate;
					this.rmdFilter.firCate = payload.firCate;
					this.rmdFilter.secCate = payload.secCate;
				} else {
					this.rmdFilter.firCate = payload.firCate;
					this.rmdFilter.secCate = payload.secCate;
					this.rmdFilter.thirCate = payload.thirCate;
				}

				this.getRmd()
			})
			// 监听筛选选择
			uni.$on("changeScreen", payload => {
				this.rmdList = [];
				this.rmdFilter.page_no = 1;
				this.hasNext = true;
				if (payload.hasOwnProperty('type')) {
					this.rmdFilter.type = payload.type
				} else {
					delete this.rmdFilter.type
				}
				if (payload.hasOwnProperty('percent')) {
					this.rmdFilter.percent = payload.percent
				} else {
					delete this.rmdFilter.percent
				}
				if (payload.hasOwnProperty('age')) {
					this.rmdFilter.age = payload.age
				} else {
					delete this.rmdFilter.age
				}
				if (payload.hasOwnProperty('price1')) {
					this.rmdFilter.price1 = payload.price1
				} else {
					delete this.rmdFilter.price1
				}
				if (payload.hasOwnProperty('price2')) {
					this.rmdFilter.price2 = payload.price2
				} else {
					delete this.rmdFilter.price2
				}
				this.getRmd()

			})

			// 自动登录
			// 判断能不能获取到本地存储的'token'
			
			if (uni.getStorageSync('token')) {	
				this.$store.dispatch("apiUserAutoLogin", {
					token: uni.getStorageSync('token')
				}).then(res => {
					
					setTimeout(() => {
						this.$store.dispatch("timLogin", res.data.data.imid).then(timRes => {
							setTimeout(() => {
								this.$store.dispatch("timEditInfo", {
									nick: res.data.data.nickname,
									role: 2
								})
							}, 1000)
						})
						let _data={
								userId:res.data.data.userId,
								city:parseInt(this.locationCity.adcode),
								version:'MP'
							};
						this.$store.dispatch("eidtUserInfo",_data)
					}, 500)
				}).catch(res => {
					 
				})
			}
			
			if(cfm.scene){
				let _sc=COMM.strToObj(decodeURIComponent(cfm.scene))
				let scene = decodeURIComponent(cfm.scene);
				console.log(scene)
				
				//-是我们分隔参数的方式
				
				let storeId = scene.split("&")[0];
				
				
				 let userId = scene.split('&')[1];
				 
				let cid = scene.split('&')[2];
				
				let actOldAndNewId = scene.split('&')[3];
				if (actOldAndNewId) {
					setTimeout(()=>{
						this.toPage({
							url:'oldfulifriend',
							storeId:storeId,
							userId:userId,
							cid:cid,
							actOldAndNewId:actOldAndNewId
							
						})
						
					},1500)
					
				}				
				if(_sc.uid){
					this.$store.commit("parentId",_sc.uid)
				}
				if(_sc.url){
					setTimeout(()=>{
						console.log(_sc)
						this.toPage(_sc)
						
					},1500)
				}
				
			}else{
				if(cfm.uid){
					this.$store.commit("parentId",cfm.uid)
				}
				if(cfm.url){
					setTimeout(()=>{
						this.toPage(cfm)
					},1500)
				}
			}

			// 获取筛选条浮动高度
			setTimeout(() => {
				let query = uni.createSelectorQuery().in(this);
				query.select('.content').boundingClientRect(data => {
					this.cntHeight = data.height
				}).exec();
			}, 1000)

			uni.$on('toPageScroll', () => {
				uni.pageScrollTo({
					scrollTop: _this.cntHeight,
					duration: 300
				});
			})
			
		},
		// 设置筛选条是否为固定定位
		onPageScroll(e) {
			if(this.top>e.scrollTop){
				this.scType=-1
			}else if(this.top<e.scrollTop){
				this.scType=1
			}
			this.top = e.scrollTop
			if (e.scrollTop >= this.cntHeight || this.$refs.screenBar.show) {
				this.isFiexd = true
			} else {
				this.isFiexd = false
			}
		},
		// 页面滚动到底部
		onReachBottom() {
			if (this.hasNext) {
				this.getRmd()
			} else {
				uni.showToast({
					title: '没有更多的数据了',
					icon: 'none'
				});
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.isFresh = false;
			this.rmdFilter.page_no = 1;
			this.hasNext = true;
			this.rmdFilter.areaId = this.locationCity.adcode.substr(0, 4);
			this.rmdList = [];
			this.loding=true;
			setTimeout(() => {
				
				this.isFresh = true;
				this.getRmd()
				
				uni.stopPullDownRefresh();
				
			}, 1500)
		}

	}
</script>

<style lang="less">
	.grEmpty{
		width: 100%;
		border-radius: 20upx;
	}
</style>
